Ziva Vatra - home :: Software :: Arduino FreeBSD

Arduino development on FreeBSD

Shortly after moving my workstation to FreeBSD, I discovered that I could not get the Arduino IDE to work properly, specifically I had two major issues:

  1. The FreeBSD Arduino IDE would not allow me to include my own source files in the sketch, so rather than keeping different logic in their own files, I was forced to munge all of it into one long, hard to maintain source file.
  2. Once compiled I was unable to update to the Arduino. I kept getting response errors no matter what I tried.

While researching online I found out that there is a FreeBSD project to allow Arduino to use Makefiles for building and uploading. I set up everything according to the official wiki page, the contents of which I will not repeat here. Once done I found that not only did my problems go away allowing me to compile and upload to my Arduino, overall I found it a nicer development experience than using the official IDE. As such I will document my personal setup which I am currently happy with.

My project structure looks like this

./Makefile
./lib/
./entry.cpp
./.syntastic_cpp_config # (optional)

As I use vim with syntastic, I have the ".syntastic_cpp_config" file mentioned above, this allows us to configure the C++ linting. We have to do this because Arduino uses some non standard locations for its header files, so without it you would get errors like "Arduino.h not found", which prevents the linting from properly working. My default syntastic config adds some include paths, and looks like this:

-i/usr/local/arduino/hardware/arduino/avr/cores/arduino -i/usr/local/avr/include/avr/

My makefile looks like this:

ARDUINO_DIR=    /usr/local/arduino
ARDUINO_MK_DIR= /usr/local/arduino-bsd-mk
ARDUINO_LIBS=  ./lib/
AVRDUDE_PORT=   /dev/cuaU0
ARDUINO_BOARD=  uno
SRCS=           ./entry.cpp
TARGET=         ./firmware

include /usr/local/arduino-bsd-mk/bsd.arduino.mk

upload: firmware.hex
	make flash

The "upload" bit is personal preference of mine, because it is more logical to type "make upload" when I want to upload the firmware than "make flash" (which is the default).

Notes:

  • Do not call your entry file "main.cpp". I originally did that and got "undefined reference to 'main'" errors.
  • The AVRDUDE_PORT value is dependent on your machine, but it will most likely start with "/dev/cuaU".
  • I have set a "./lib" folder, this allows me to hold project libraries separate from the main code. These project libraries are not the Arduino libraries, those get included from a different location. For the moment I don't have a strong need for code reuse between projects so each project has a local "lib" folder.
  • I've set TARGET to "firmware", so after a successful "make" I get a "firmware.hex" binary ready for upload

I discovered that the BSD makefile builds source that is compliant to C "gnu99" and C++ "c++11" standards. This is good to know, especially for C++ as now I know what C++ features I can use. While it would be tempting to try a newer C++ standard I suspect if I were to change that a lot of the third party Arduino libraries will break. As those libraries are one of the main perks of using an Arduino in the first place I think I will leave things as is.

That is it for now as it is still early days for me and the new build system. If and when I come up with improvements I will post them here.

Page created: Thu Oct 10 23:23:13 2024 ][ Page last modified: Sat Oct 18 16:08:42 2025 ]